package com.homedo.microservice.open.service.gateway.alert.gateway.persistence.dao;

import com.google.common.collect.Lists;
import com.homedo.common.dao.BaseDao;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.po.Config;
import com.homedo.microservice.open.service.gateway.alert.gateway.persistence.mapper.IConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月11日 17时25分30秒
 */
@Repository
public class ConfigDao extends BaseDao<Config> {
    @Autowired
    private IConfigMapper configMapper;

    public List<Config> getListByApplication(String applicationName) {
        Example example = new Example(Config.class);
        example.createCriteria().andEqualTo(Config.FIELD_APPLICATIONNAME, applicationName);
        return super.getListByExample(example);
    }

    public Config getListByApplication(String applicationName, String name) {
        List<Config> configs = this.getListByApplication(applicationName, Lists.newArrayList(name));

        if (!Detect.notEmpty(configs)) {
            return null;
        }

        return configs.get(0);
    }

    public List<Config> getListByApplication(String applicationName, List<String> names) {
        Example example = new Example(Config.class);
        example.createCriteria().andEqualTo(Config.FIELD_APPLICATIONNAME, applicationName).andIn(Config.FIELD_NAME, names);
        example.setOrderByClause(Config.ORDER_CREATE_TIME_DESC);
        return super.getListByExample(example);
    }

}
